package org.jeecg.modules.door.stocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.stereotype.Component;

@Component
public class RedisSubscriber implements MessageListener {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private ChannelTopic topic;

    /*public RedisSubscriber(String topicName) {
        this.topic = new ChannelTopic(topicName);
    }*/

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String topic = new String(pattern);
        String body = new String(message.getBody());
        System.out.println("Received message on topic '" + topic + "': " + body);
        // 在这里处理接收到的消息
    }

/*    public void subscribe() {
        redisTemplate.convertAndSend(topic.getTopic(), "dummy message to initialize subscription"); // 发送一个空消息以触发订阅
        redisTemplate.getMessageListenerContainer().addMessageListener(this, topic);
    }*/
}
